package com.comdosoft.comdoandroidbase;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import java.util.LinkedList;
import java.util.List;

public class MyApplication extends Application {
	public static Context context;
	public static MyApplication instance = null;
	private List<Activity> activities = new LinkedList<Activity>();

	public void onCreate() {
		super.onCreate();
		context = this;

		initImageLoader(this);
	}

	public synchronized static MyApplication getInstance() {
		if (null == instance) {
			instance = new MyApplication();
		}
		return instance;
	}

	public void addActivity(Activity activity) {
		activities.add(activity);
	}

	public void finishAll() {
		for (Activity activity : activities) {
			if (!activity.isFinishing()) {
				activity.finish();
			}
		}
	}

	public static void initImageLoader(Context context) {
		// 缓存图片
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				context).threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.writeDebugLogs() // Remove for release app
				.build();
		ImageLoader.getInstance().init(config);
		ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context));
	}

	public static DisplayImageOptions getDisplayDefaultOption() {
		DisplayImageOptions options = new DisplayImageOptions.Builder()
		.showStubImage(R.mipmap.moren)// 设置图片下载期间显示的图片
		.showImageForEmptyUri(R.mipmap.moren)	// 设置图片Uri为空或是错误的时候显示的图片
		.showImageOnFail(R.mipmap.moren)// 设置图片加载或解码过程中发生错误显示的图片
		.cacheInMemory(true) // 设置下载的图片是否缓存在内存中
		.cacheOnDisc(true).build(); // 设置下载的图片是否缓存在SD卡中

		return options;
	}
	/**
	 * 隐藏软键盘
	 * @param activity
	 */
	public static void hideSoftKeyboard(Activity activity){
		View view = activity.getWindow().peekDecorView();
		if (view != null) {
			InputMethodManager inputmanger = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
			inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
		}
	}

	/**
	 * 显示软键盘
	 * @param activity
	 */
	public static void showSoftKeyboard(Activity activity,EditText editText){
		try {
			InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
			inputMethodManager.showSoftInput(editText, 0);
		} catch (Exception e) {
			Log.d("Keyboard", "visible");
		}
	}
}
